package com.tring.ysyn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tring.ysyn.entity.TbEcReport;
import com.tring.ysyn.entity.TbSapReport;
import com.tring.ysyn.entity.TbYthReport;
import com.tring.ysyn.mapper.TbYthReportMapper;
import com.tring.ysyn.model.vo.LineVo;
import com.tring.ysyn.service.ITbEcReportService;
import com.tring.ysyn.service.ITbSapReportService;
import com.tring.ysyn.service.ITbYthReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tring
 * @since 2023-03-25
 */
@Service
@Slf4j
public class TbYthReportServiceImpl extends ServiceImpl<TbYthReportMapper, TbYthReport> implements ITbYthReportService {

    @Autowired
    private ITbSapReportService sapReportService;
    @Autowired
    private ITbEcReportService ecReportService;

    @Override
    @Transactional
    public void dataIntegration() {

        this.baseMapper.delYthReport();
        List<LineVo> lineVos = this.baseMapper.getVbelns(3);
        //所有需要插入的一体化的数据
        List<TbYthReport> saveBacth = new ArrayList<>();
        StopWatch sw = new StopWatch();


        if (lineVos != null && !lineVos.isEmpty()) {
            sw.start("sapAndEcSelect");
            for (LineVo lineVo : lineVos) {
                //获取sap数据
                LambdaQueryWrapper<TbSapReport> sapWrapper = new LambdaQueryWrapper<>();
                sapWrapper.eq(TbSapReport::getVbeln, lineVo.getVbeln());
                sapWrapper.eq(TbSapReport::getPosnr, lineVo.getPosnr());
                sapWrapper.orderByAsc(TbSapReport::getEdatu);
                sapWrapper.orderByAsc(TbSapReport::getVbeln);
                List<TbSapReport> sapList = sapReportService.list(sapWrapper);

                if (sapList != null && !sapList.isEmpty()) {
                    //获取ec数据
                    LambdaQueryWrapper<TbEcReport> ecWrapper = new LambdaQueryWrapper<>();
                    ecWrapper.eq(TbEcReport::getSapNo, lineVo.getVbeln());
                    ecWrapper.eq(TbEcReport::getSapLineId, lineVo.getPosnr());
                    ecWrapper.orderByAsc(TbEcReport::getLastOutDate);
                    ecWrapper.orderByAsc(TbEcReport::getSapNo);
                    List<TbEcReport> ecList = ecReportService.list(ecWrapper);

                    //欠出库数
                    int qlfimg = 0;
                    //系统分配出库数
                    Integer sysShipQty = 0;

                    for (TbSapReport sap : sapList) {
                        //ec第一条数据剩余库存数
                        int ecSurplusCount = 0;

                        qlfimg = Integer.parseInt(sap.getQlfimg());
                        TbYthReport ythReport = new TbYthReport();
                        if (ecList != null && !ecList.isEmpty()) {
                            for (TbEcReport ec : ecList) {
                                //库存初始化
                                if(ecSurplusCount == 0){
                                    ecSurplusCount = ec.getShipQty();
                                }
                                //如果没有欠，则跳出ec循环
                                if (qlfimg <= 0) {
                                    break;
                                }

                                BeanUtils.copyProperties(ec, ythReport);
                                BeanUtils.copyProperties(sap, ythReport);
                                //默认是够分配,则系统分配数额为订单欠额
                                ythReport.setSysShipQty(qlfimg);
                                //如果不够分，把当前ec的全部先分了，继续往下拿数据
                                if (qlfimg - ecSurplusCount>=0){
                                    ecList.remove(ec);
                                    ythReport.setSysShipQty(ecSurplusCount);
                                }

                                //默认不是新增行
                                ythReport.setAddFlag(0);
                                //若果ec出库分配还有剩余的，就系分配给下个sap，则为新增行
                                if(ecSurplusCount != ec.getShipQty()){
                                    ythReport.setAddFlag(1);
                                }
                                saveBacth.add(ythReport);
                                //计算还欠多少出货数
                                qlfimg = qlfimg - ecSurplusCount;
                                //如果没有欠出库数 并且 出库数已经全部出库，则移除此出库记录
                                if (qlfimg <= 0 && ecSurplusCount<=0){
                                    ecSurplusCount = ec.getShipQty()-ecSurplusCount;
                                    ecList.remove(ec);
                                }
                                //如果ec没有数据则跳出ec循环
                                if(ecList.isEmpty()){
                                    break;
                                }
                            }
                        } else {
                            //插入一体化数据
                            BeanUtils.copyProperties(sap, ythReport);
                            ythReport.setSysShipQty(sysShipQty);
                            ythReport.setAddFlag(0);
                            saveBacth.add(ythReport);
                        }
                    }
                }
            }
            sw.stop();
        }


        //处理sap有数据，ec没有的数据放入一体化数据集合中
        sw.start("sapOtherList");
        List<TbYthReport> sapOtherList = this.baseMapper.getSapOtherData();
        sw.stop();
        if (sapOtherList != null && !sapOtherList.isEmpty()) {
            saveBacth.addAll(sapOtherList);
        }

        //批量插入
        sw.start("insertYthData");
        if(!saveBacth.isEmpty()){
            this.baseMapper.insertBatchSomeColumn(saveBacth);
        }
        sw.stop();
        log.info(sw.prettyPrint());
    }
}
